package cn.wolfcode.edu.service.impl;

import cn.wolfcode.edu.domain.Salary;
import cn.wolfcode.edu.mapper.SalaryMapper;
import cn.wolfcode.edu.query.SalaryQueryObject;
import cn.wolfcode.edu.service.ISalaryService;
import cn.wolfcode.edu.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class SalaryServiceImpl implements ISalaryService {
    @Autowired
    private SalaryMapper salaryMapper;
    //查询列表方法(分页 + 高级查询)
    public PageResult list(SalaryQueryObject qo) {
        int total = salaryMapper.queryForCount(qo);
        if(total==0){
            return new PageResult();
        }
        List<Salary> rows = salaryMapper.selectAll(qo);
        return new PageResult(total,rows);
    }
    //删除方法
    public void delete(Long id) {
        salaryMapper.deleteByPrimaryKey(id);
    }
    //编辑更新方法
    public void update(Salary salary) {
        salaryMapper.updateByPrimaryKey(salary);
    }
    //查询列表(高级查询)
    public List queryForList(SalaryQueryObject qo) {
        return salaryMapper.selectAll(qo);
    }
    //核算方法
    public void account(Long id) {
        Salary salary = salaryMapper.selectByPrimaryKey(id);
        Integer earlyDays = salary.getEarlyDays();
        Integer lateDays = salary.getLateDays();
        BigDecimal salarySalary = salary.getSalary();
        Integer workDays = salary.getWorkDays();
        BigDecimal i = BigDecimal.valueOf(lateDays * 20 + earlyDays * 50);
        BigDecimal finalSalary = salarySalary.subtract(i);
        if(workDays == 0){
            finalSalary = BigDecimal.ZERO;
        }
        salary.setFinalSalary(finalSalary);
        salary.setStockDater(new Date());
        salaryMapper.account(salary);
    }

    public Salary selectByPrimaryKey(Long id) {
        return salaryMapper.selectByPrimaryKey(id);
    }

    public void insertFromFile(Salary salary) {
        salaryMapper.insert(salary);
    }

    public void save(Salary salary) {
        salaryMapper.insert(salary);
    }
}
